package com.gdones.gpt.bus.modetemplate.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.gdones.common.core.dto.MyPageBand;
import com.gdones.common.core.dto.MyPager;
import com.gdones.common.core.dto.MyResult;
import com.gdones.common.core.util.MyPinYinUtil;
import com.gdones.gpt.bus.modes.dto.GptModes;
import com.gdones.gpt.bus.modetemplate.dto.GptTemplate;
import com.gdones.gpt.bus.modetemplate.dto.GptTemplateDto;
import com.gdones.gpt.bus.modetemplate.dto.GptTemplateInfo;
import com.gdones.gpt.bus.modetemplate.mapper.GptTemplateInfoMapper;
import com.gdones.gpt.bus.modetemplate.mapper.GptTemplateMapper;
import com.gdones.gpt.bus.modetemplate.service.IGptTemplateInfoService;
import com.gdones.gpt.bus.modetemplate.service.IGptTemplateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gdones.gpt.core.util.MySysUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author GaoJingBo
 * @since 2023-06-02
 */
@Service
public class GptTemplateServiceImpl extends ServiceImpl<GptTemplateMapper, GptTemplate> implements IGptTemplateService {

    @Autowired
    private GptTemplateMapper templateMapper;

    @Autowired
    private GptTemplateInfoMapper infoMapper;

    @Resource(name = "gptTemplateInfoServiceImpl")
    private IGptTemplateInfoService infoService;

    @Override
    public MyResult pages(GptTemplate dto, MyPageBand pageBand) {

        MyResult result = new MyResult();

        IPage<GptTemplate> pages = templateMapper.pages(PageDTO.of(pageBand.getPage(), pageBand.getSize()), dto);

        // 根据分页的结果封装返回数据
        MyPager<GptTemplate> pager = new MyPager<>();
        pager.setTotal(pages.getTotal());
        pager.setPage(pageBand.getPage());
        pager.setPageSize(pageBand.getSize());
        pager.setRows(pages.getRecords());

        result.setData(pager);
        result.setMsg("查询成功！");

        return result;
    }

    @Override
    public MyResult mylist(GptTemplate dto) {

        MyResult result = new MyResult();

        List<GptTemplate> list = templateMapper.myList(dto);

        result.setData(list);
        result.setMsg("查询成功！");

        return result;
    }

    @Override
    public MyResult getByID(String id) {

        MyResult result = new MyResult();

        GptTemplate gptTemplate = templateMapper.selectById(id);

        QueryWrapper<GptTemplateInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("templateID",id);
        queryWrapper.orderByAsc("mindex");

        List<GptTemplateInfo> gptTemplateInfos = infoMapper.selectList(queryWrapper);

        GptTemplateDto dto = new GptTemplateDto();
        dto.setTemplate(gptTemplate);
        dto.setInfos(gptTemplateInfos);

        result.setMsg("查询成功！");
        result.setData(dto);

        return result;
    }

    @Override
    public MyResult add(GptTemplateDto dto) {

        MyResult result = new MyResult();

        GptTemplate template = dto.getTemplate();
        template.setTemplateID(IdUtil.fastSimpleUUID());
        template.setCode("TEMP-"+ MyPinYinUtil.getPinYinByString(template.getName())+"-"+new Date().getTime());

        List<GptTemplateInfo> infos = dto.getInfos();
        for(GptTemplateInfo info : infos){
            info.setTemplateID(template.getTemplateID());
        }

        int insert = templateMapper.insert(template);
        if(insert>0){
            boolean b = infoService.saveBatch(infos);
            if(b){
                result.setMsg("模板保存成功！");
            }else{
                result.setMsg("模板保存错误！");
                result.setCode("9999");
            }
        }else{
            result.setMsg("模板保存错误！");
            result.setCode("9999");
        }

        return result;
    }

    @Override
    public MyResult update(GptTemplateDto dto) {

        MyResult result = new MyResult();

        GptTemplate template = dto.getTemplate();
        boolean b = this.updateById(template);
        if(b){

            // 先删除旧的数据
            int dels = infoMapper.realDeleteByTemplateID(template.getTemplateID());
            List<GptTemplateInfo> infos = dto.getInfos();
            for(GptTemplateInfo info : infos){
                info.setTemplateID(template.getTemplateID());
            }
            boolean bath = infoService.saveBatch(infos);
            if(bath){
                result.setMsg("模板修改成功！");
            }else{
                result.setMsg("模板修改错误！");
                result.setCode("9999");
            }
        }else{
            result.setMsg("模板修改错误！");
            result.setCode("9999");
        }

        return result;
    }

    @Override
    public MyResult deletes(String ids) {

        MyResult result = new MyResult();

        String[] split = ids.split(",");

        List<String> list = Arrays.asList(split);

        // 查询集合对应id的数据看是否有选中未激活注册模板
        QueryWrapper<GptTemplate> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("templateID",list);

        List<GptTemplate> gptTemplates = templateMapper.selectList(queryWrapper);

        boolean isHave = false;
        String ttname = "";
        for(GptTemplate template : gptTemplates){
            if("1".equals(template.getIsNewStaffTemplate())){
                ttname = template.getName();
                isHave = true;
                break;
            }
        }

        if(isHave){
            result.setCode("9999");
            result.setMsg("["+ttname+"]模板为新用户注册未激活模板，不可删除！");
            return result;
        }

        boolean isErr = false;

        for(String id : list){
            int i = templateMapper.deleteById(id);
            if(i>0){
                infoMapper.realDeleteByTemplateID(id);
            }else{
                isErr = true;
                break;
            }
        }

        if(isErr){
            result.setMsg("模板删除错误！");
            result.setCode("9999");
        }else{
            result.setMsg("模板删除成功！");
        }

        return result;
    }

    @Override
    public MyResult setDefaultNewStaffTemplate(String id) {

        MyResult result = new MyResult();

        // 先将所有的模板设置为0
        QueryWrapper<GptTemplate> queryWrapper = new QueryWrapper<>();
        GptTemplate updateBo = new GptTemplate();
        updateBo.setIsNewStaffTemplate("0");
        int update = templateMapper.update(updateBo, queryWrapper);

        // 再将当前更新为1
        GptTemplate updateBo2 = new GptTemplate();
        updateBo2.setTemplateID(id);
        updateBo2.setIsNewStaffTemplate("1");

        int i = templateMapper.updateById(updateBo2);
        if(i>0){
            result.setMsg("设置成功！");
        }else{
            result.setMsg("设置修改出错！");
            result.setCode("9999");
        }

        return result;
    }
}
